package cn.easybuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.easybuy.entity.Product;
import cn.easybuy.service.ProductService;
import cn.easybuy.service.impl.ProductServiceImpl;
import cn.easybuy.util.ShoppingCart;
import cn.easybuy.util.ShoppingCartItem;

/**
 * Servlet implementation class CartServlet
 */
@WebServlet("/CartServlet")
public class CartServlet extends HttpServlet {
	
	ProductService productService =null; //商品的service
	
	@Override
	public void init() throws ServletException {
		productService =new ProductServiceImpl();
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		System.out.println("------------------进入到CartServlet");
		
		String action = request.getParameter("opr");
		switch (action) {
			case "addCart":
				addCart(request,response);
				break;
				
			case "refleshCart":
				refleshCart(request,response);
				break;
			default:
				break;
		}
		
	}

	private void refleshCart(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		//去session找到最新的购物车信息,如果session里面没有,就不更新
		HttpSession session  =request.getSession();//获取session对象
		
		ShoppingCart cart=(ShoppingCart) session.getAttribute("cart");//获取购物车
		if(cart!=null && cart.getItems().size()>0) {//至少有一样商品
			System.out.println("----refleshCart"+cart.getItems().size());
			 cart.caclSum();
		}
		
	}

	private void addCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		//找到用户的购物车 --start
		
		//添加到购物车  把数据存到session中
		HttpSession session  =request.getSession();//获取session对象
		PrintWriter out =response.getWriter(); //out对象
		
		ShoppingCart cart = null;
		
		cart =(ShoppingCart) session.getAttribute("cart"); //第一次执行是null,如果不是null
		
		if(cart ==null ) {
			cart =new ShoppingCart();// 确保每个用户进入系统后,有个自己的购物车
			session.setAttribute("cart", cart);
		}
		//找到用户的购物车 --end
		System.out.println("购物车初始化的的数量: "+ cart.getItems().size());
		
		//往购物车中添加商品
		Integer pid = Integer.valueOf(request.getParameter("pid"));
		Integer count = Integer.valueOf(request.getParameter("count"));
		
		// 根据id 查找商品明细
		Product product = productService.getProductById(pid);
		
		/////-----难点
		cart.addProToCart(product,count); //添加商品到购物车
		/////-----难点
		
		//添加成功后更新session的数据
		if(cart!=null) {
			session.removeAttribute("cart");
		}
		session.setAttribute("cart", cart);
		
		
		System.out.println("添加后购物车的数量: "+ cart.getItems().size());
		
		out.print("添加成功");
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
